﻿/*
Copyright © 2010 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace MSBuild.Mercurial.CodePlex
{
    /// <summary>
    /// Utility methods to generate the documentation of the MSBuild tasks.
    /// </summary>
    public static class DocumentationBuilder
    {
        /// <summary>
        /// The reference used to determine if a class is a MSBuild task.
        /// </summary>
        private static readonly Type Reference = typeof(ITask);

        /// <summary>
        /// Extracts the MSBuild task defined in an assembly.
        /// </summary>
        /// <param name="assembly">
        /// The reference assembly for the extraction.
        /// </param>
        /// <returns>
        /// A collection of type that represents MSBuild tasks.
        /// </returns>
        public static ICollection<Type> RetrieveTasks(Assembly assembly)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }

            ICollection<Type> result = new LinkedList<Type>();
            foreach (Type type in assembly.GetTypes())
            {
                if (Reference.IsAssignableFrom(type) && type.IsPublic && !type.IsAbstract)
                {
                    result.Add(type);
                }
            }

            return result;
        }

        /// <summary>
        /// Creates the documentation associated with a specific task definition.
        /// </summary>
        /// <param name="taskType">
        /// The type of the task defintion to document.
        /// </param>
        /// <returns>
        /// The documentation of the task.
        /// </returns>
        public static TaskDocumentation CreateDocumentation(Type taskType)
        {
            if (taskType == null)
            {
                throw new ArgumentNullException("taskType");
            }

            TaskDocumentation result = new TaskDocumentation();
            result.Name = taskType.Name;
            result.Comment = taskType.Comment();

            BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty;
            foreach (PropertyInfo property in taskType.GetProperties(flags))
            {
                PropertyDocumentation doc = CreateDocumentation(property);
                if (doc != null)
                {
                    result.Properties.Add(doc);
                }
            }

            return result;
        }

        /// <summary>
        /// Creates the documentation associated with a specific property.
        /// </summary>
        /// <param name="property">
        /// The property to document.
        /// </param>
        /// <returns>
        /// The documentation of the property, or <c>null</c> if the property
        /// is not an MSBuild parameter.
        /// </returns>
        public static PropertyDocumentation CreateDocumentation(PropertyInfo property)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            if (property.DeclaringType == typeof(Task))
            {
                return null;
            }

            PropertyDocumentation result = new PropertyDocumentation();
            result.Name = property.Name;
            result.Comment = property.Comment();
            result.Type = ConvertPropertyType(property.PropertyType);

            foreach (object attribute in property.GetCustomAttributes(true))
            {
                if (attribute is RequiredAttribute)
                {
                    result.Required = true;
                }

                if (attribute is OutputAttribute)
                {
                    result.Output = true;
                }

                ParameterAttribute parameter = attribute as ParameterAttribute;
                if (parameter != null)
                {
                    result.Parameter = parameter.Prefix;
                }
            }

            return result;
        }

        /// <summary>
        /// Converts a type used by a property to a string representation.
        /// </summary>
        /// <param name="propertyType">
        /// The type of the property.
        /// </param>
        /// <returns>
        /// The string representation associated with the type.
        /// </returns>
        private static string ConvertPropertyType(Type propertyType)
        {
            if (propertyType == typeof(string))
            {
                return "string";
            }
            else if (propertyType == typeof(bool))
            {
                return "choice";
            }
            else if (propertyType == typeof(int))
            {
                return "int";
            }
            else if (propertyType == typeof(DateTime?))
            {
                return "date";
            }
            else if (propertyType == typeof(ITaskItem[]))
            {
                return "item[]";
            }
            else
            {
                return "unknown";
            }
        }
    }
}
